Skip to content

fix(cli): resume older working-directory sessions in /sessions and --continue - #2952

Open
creatiVision wants to merge 3 commits into
MoonshotAI:mainfrom
creatiVision:fix/session-workdir-resume
Open

fix(cli): resume older working-directory sessions in /sessions and --continue#2952
creatiVision wants to merge 3 commits into
MoonshotAI:mainfrom
creatiVision:fix/session-workdir-resume

Conversation

@creatiVision

Copy link
Copy Markdown

Problem

Since the v2 engine became the CLI default (#2627), /sessions (working-directory scope) and kimi --continue stop surfacing the older sessions of the current working directory — most notably legacy/v1-era sessions recorded with only workDir (no cwd). /sessions shows an empty cwd list and --continue silently starts a fresh session.

Related: #2951 (report), #1650 (open, describes the same bucket-coordination gap).

What changed

Two spots covered:

  1. v2 session index read-model miss on a workspace-scoped listRecent (packages/agent-core-v2/src/app/sessionIndex/sessionIndexService.ts): when the minidb read model answers an empty page for a workspaceIds-scoped query, coalesce onto the authoritative directory read so sessions that exist on disk but were not projected (legacy workDir-only, alias buckets) are never hidden. This fixes the TUI /sessions working-directory scope.

  2. --continue matching (apps/kimi-code/src/cli/v2/run-v2-print.ts): normalize the cwd === workDir comparison with resolve(...) (matching the existing --session guard) so path variations (symlink, trailing slash, case) no longer drop the last session.

Changed

  • packages/agent-core-v2/src/app/sessionIndex/sessionIndexService.ts
  • apps/kimi-code/src/cli/v2/run-v2-print.ts
  • test in packages/agent-core-v2/test/app/sessionIndex/sessionIndex.test.ts
  • changeset @moonshot-ai/kimi-code patch

Out of scope

Checklist

  • Explain the problem
  • Describe what changed
  • Added a unit test covering the read-model fallback

@changeset-bot

changeset-bot Bot commented Aug 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 49ca402

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5abe8379c8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/app/sessionIndex/sessionIndexService.ts
Comment on lines +331 to +335
// A workspace-scoped read that returns nothing can still hide sessions
// that exist on disk but were not projected into the read model (e.g.
// legacy/v1-era sessions recorded with only `workDir`, or sessions in
// alias buckets). Coalesce onto the authoritative source so `/sessions`
// (working-directory scope) and `--continue` never hide them. Unscooped

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move method-body narration into the file header

This newly added explanatory block sits inside listRecent and narrates the implementation, while the scoped v2 guide requires comments to live solely in the top-of-file /** */ block and describe external responsibilities. Remove the inline narration or fold the relevant responsibility-level context into the existing module header.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L36-L38

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 67f00e0

@creatiVision
creatiVision force-pushed the fix/session-workdir-resume branch from ecf7d01 to bb75ffd Compare September 7, 2026 07:10
@creatiVision

Copy link
Copy Markdown
Author

Rebased onto latest main. The branch had previously absorbed merge commits during fork maintenance; it has now been cleanly rebased on top of upstream main to focus strictly on the original PR intent:

  1. packages/agent-core-v2/src/app/sessionIndex/sessionIndexService.ts: Coalesce empty read-model pages for workspace-scoped listRecent queries onto listLegacy so legacy workDir-only and alias-bucket sessions on disk are never hidden in /sessions.
  2. apps/kimi-code/src/cli/v2/run-v2-print.ts: Normalize the cwd vs workDir comparison with resolve(...) so path variations (symlinks, trailing slashes) no longer drop the latest session on --continue.
  3. Unit test in sessionIndex.test.ts added and verified green, with zero comments in agent-core-v2 per repository rules.
  4. Changeset included.

The PR is now clean, mergeable, and ready for review.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bdeb5cf089

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

const page = await index.listRecent({});
const previous = page.items.find((summary) => summary.cwd === workDir);
const previous = page.items.find(
(summary) => summary.cwd !== undefined && resolve(summary.cwd) === resolve(workDir),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Compare Windows paths case-insensitively

When the same Windows working directory is entered with different component casing (for example, a stored C:/Users/Alice/Repo versus a current C:/users/alice/repo), resolve() makes the paths absolute and normalizes their syntax but preserves that casing, so this equality still fails and --continue silently starts a new session. Use the repository's Windows-aware workspace-root normalization, or otherwise case-fold Windows-shaped paths, before comparing them.

Useful? React with 👍 / 👎.

Comment on lines +451 to +453
const previous = page.items.find(
(summary) => summary.cwd !== undefined && resolve(summary.cwd) === resolve(workDir),
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate cached cwd before resolving it

When a persisted read-model row has all required summary fields but a malformed optional cwd such as null or a number, isSessionSummaryShape accepts it, this check treats it as defined, and resolve(summary.cwd) can throw instead of skipping the unusable session. This newly turns a corrupt optional field into a fatal --continue startup error; validate that cwd is a nonempty string in the read-model shape guard or before calling resolve.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 49ca402013

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

const page = await index.listRecent({});
const previous = page.items.find((summary) => summary.cwd === workDir);
const previous = page.items.find(
(summary) => summary.cwd !== undefined && resolve(summary.cwd) === resolve(workDir),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve symlink aliases before comparing work directories

When a session was created through the SDK/API with a symlinked workDir (for example, /work/repo-link) and kimi --continue is later launched from the symlink target (/srv/repo), this still starts a fresh session. resolve() only performs lexical path normalization and leaves the symlink component intact, so the two paths remain unequal; use filesystem canonicalization such as realpath before comparing, with an appropriate fallback for unavailable paths.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant